new Vue({
  el: '#app',
  data() {
    return {
      form: {
        page: 1,			//页数
        limit: 10			//每页数据条数
      },
      formData: {},
      tableData: [{}],		//表格数据
      total: 1,
      dialogForm: false,
      title: '',
      multipleSelection: [],
      Sales_Order_SerialNumber: 0,
      dialogVisible: false,
      checked: false,
    }
  },
  mounted() {
    this.getList(this.form)
  },
  computed: {
    // formatTime() {
    //   return function (time) {
    //     let d = new Date(parseInt(time));
    //     return `${d.getFullYear()}-${d.getMonth() + 1
    //       }-${d.getDate()} ${d.getHours()}:${d.getMinutes()}:${d.getSeconds()}`;
    //   };
    // },
  },
  methods: {
    onSerach() {				//查询方法
      console.log("搜搜")
    },
    handleSizeChange(val) {
      this.form.page = 1;
      this.form.limit = val;
      this.getList(this.form)
    },
    handleCurrentChange(val) {
      this.form.page = val;
      this.getList(this.form)
    },
    getList(data) {
      let _this = this
      $.ajax({
        type: 'POST',
        data,
        url: DomainName + '/getRefundAction?token=' +  $.cookie("token"),
        success: function (res) {
          _this.tableData = res.tableData;
          _this.total = parseInt(res.limit)
          console.log(res);
        },
        complete: function (XMLHttpRequest) {
          _this.loading = false
          if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
            window.parent.document.location.href = "../../../" + XMLHttpRequest.getResponseHeader("CONTENTPATH");
          }
        },
        dataType: "json"
      });
    },
    addEnterprise() {
      window.location.href = './Add.html'
    },
    accMul(arg1, arg2) {
      let m = 0, s1 = arg1.toString(), s2 = arg2.toString();
      try { m += s1.split(".")[1].length } catch (e) { }
      try { m += s2.split(".")[1].length } catch (e) { }
      return Number(s1.replace(".", "")) * Number(s2.replace(".", "")) / Math.pow(10, m)
    },
    // 新通过退款confirm_methods
    isRefund(Sales_Order_SerialNumber) {
      this.dialogVisible = true
      this.Sales_Order_SerialNumber = Sales_Order_SerialNumber;
    },
    confirm_methods() {
      let Sales_Order_SerialNumber = this.Sales_Order_SerialNumber;
      let isDelivery = this.checked ? '1' : '0';
      let _this = this
      $.ajax({
        type: 'POST',
        data: {
          Sales_Order_SerialNumber,
          Type: '1',
          token: $.cookie("token"),
          isDelivery
        },
        url: DomainName + '/getChatRefundCheckAction',
        success: function (data) {
          console.log(data)
          _this.dialogVisible = false
          if (data.Status == "success") {
            _this.$message({
              type: 'success',
              message: '退款成功'
            });
            _this.getList(_this.form)
          } else if (data.Status == "PROCESSING") {
            _this.$message({
              type: 'success',
              message: '退款已提交,等待审核'
            });
            _this.getList(_this.form)

          }
        },
        complete: function (XMLHttpRequest) {
          _this.loading = false
          if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
            window.parent.document.location.href = "../../../" + XMLHttpRequest.getResponseHeader("CONTENTPATH");
          }
        },
        dataType: "json"
      });

      // this.$confirm('通过退款申请，将开始退款, 是否继续?', '提示', {
      //   confirmButtonText: '确定',
      //   cancelButtonText: '取消',
      //   type: 'warning',
      //   center: true
      // }).then(() => {

      // }).catch(() => {
      //   this.$message({
      //     type: 'info',
      //     message: '已取消！'
      //   });
      // });
    },
    // 新拒绝退款
    isNorefund(Sales_Order_SerialNumber) {
      let _this = this
      this.$prompt('请输入拒绝原因，如线下沟通则说明', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        inputPattern: /^(?!\s*$).+/,
        inputErrorMessage: '原因不能由零个或多个空格组成',
        type: 'warning',
      }).then(({ value }) => {
        $.ajax({
          type: 'POST',
          url: DomainName + '/getChatRefundCheckAction',
          data: {
            Sales_Order_SerialNumber,
            Type: '2',
            Cause: value,
            token: $.cookie("token")
          },
          success: function (data) {
            if (data.Status == "success") {
              _this.$message({
                type: 'info',
                message: '已拒绝申请！'
              });
              _this.getList1(_this.form)
            }
          },
          complete: function (XMLHttpRequest) {
            _this.loading = false
            if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
              window.parent.document.location.href = "../../../" + XMLHttpRequest.getResponseHeader("CONTENTPATH");
            }
          },
          dataType: "json"
        });
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消！'
        });
      });
    },
    // 旧通过退款
    refund(Sales_Order_SerialNumber) {
      let _this = this
      this.$confirm('通过退款申请，将开始退款, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
        center: true
      }).then(() => {
        $.ajax({
          type: 'POST',
          data: {
            Sales_Order_SerialNumber,
          },
          url: DomainName + '/CollectionById',
          success: function (data) {
            if (data.length == 1) {
              _this.FuyouRefund(data[0], Sales_Order_SerialNumber);
            } else {
              _this.$message({
                type: 'info',
                message: '该订单未付款！'
              });
            }
          },
          complete: function (XMLHttpRequest) {
            _this.loading = false
            if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
              window.parent.document.location.href = "../../../" + XMLHttpRequest.getResponseHeader("CONTENTPATH");
            }
          },
          dataType: "json"
        });
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消！'
        });
      });
    },
    // 旧拒绝退款
    Norefund(Sales_Order_SerialNumber) {
      let _this = this
      this.$prompt('请输入拒绝原因，如线下沟通则说明', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        inputPattern: /^(?!\s*$).+/,
        inputErrorMessage: '原因不能由零个或多个空格组成',
        type: 'warning',
      }).then(({ value }) => {
        $.ajax({
          type: 'POST',
          data: {
            sales_order: Sales_Order_SerialNumber,
            cause: value
          },
          url: DomainName + '/NoClientRefund',
          success: function (data) {
            if (data.Status == "success") {
              _this.$message({
                type: 'info',
                message: '已拒绝申请！'
              });
              _this.getList1(_this.form)
            }
          },
          complete: function (XMLHttpRequest) {
            _this.loading = false
            if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
              window.parent.document.location.href = "../../../" + XMLHttpRequest.getResponseHeader("CONTENTPATH");
            }
          },
          dataType: "json"
        });
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消！'
        });
      });
    },
    FuyouRefund(data, Sales_Order_SerialNumber) {
      let _this = this
      $.ajax({
        type: 'POST',
        data: {
          mchnt: data.mchnt_order_no,//商户订单号
          Sales_Order_SerialNumber,//销售订单，用于查询退款日志获取退款单号
        },
        url: DomainName + '/WXWeChatRefundForMiniProgramsAction',
        success: function (data) {
          console.log(data);
          if (data.result_msg === "SUCCESS") {
            _this.$message({
              type: 'success',
              message: '退款提交成功!'
            });
            _this.getList1(_this.form)
          } else if (data.result_msg === "PROCESSING") {
            _this.$message({
              type: 'success',
              message: '退款处理中!'
            });
            _this.getList1(_this.form)
          } else if (data.result_code === "1011") {
            _this.$message({
              type: 'info',
              message: '该订单已全额退款！'
            });
            _this.getList1(_this.form)
          } else {
            _this.$message({
              type: 'info',
              message: '退款失败！'
            });
          }
        },
        complete: function (XMLHttpRequest) {
          _this.loading = false
          if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
            window.parent.document.location.href = "../../../" + XMLHttpRequest.getResponseHeader("CONTENTPATH");
          }
        },
        dataType: "json"
      });
    },
    showMember(Sales_Order_SerialNumber) {
      sessionStorage.setItem("Sales_Order_SerialNumber", Sales_Order_SerialNumber)
      window.location.href = './show.html?Sales_Order_SerialNumber=' + Sales_Order_SerialNumber
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
      console.log(this.multipleSelection);
    },
  },
})